
###################
# Compile flags
###################

ifeq ($(00_target),debug)
01_compile_flags=$(coc_compile_debug_flags)
else
01_compile_flags=$(coc_compile_release_flags)
endif


###################
# Calculating variable
###################

# -D directory variables
01_directory_minus_d=-DDIR_BINS=\"$(00_dir_product_bin)\" \
                  -DDIR_LIBS=\"$(00_dir_product_lib)\" \
                  -DDIR_INPUT=\"$(00_dir_product_input)\" \
                  -DDIR_CFG=\"$(00_dir_product_cfg)\" \
                  -DDIR_OUTPUT=\"$(00_dir_product_output)\" \
                  -DDIR_LOG=\"$(00_dir_product_log)\" \
                  -DDIR_TMP=\"$(00_dir_product_tmp)\" \
                  -DDIR_DOC=\"$(00_dir_product_doc)\" \
                  -DDIR_TEST=\"$(00_dir_dev_test)\"

# All the internal Projects that the current Project depends on
01_prj_depends:=$(project_prj_int_static_libs) $(project_prj_int_shared_libs) $(project_prj_int_just_copy) $(project_prj_int_pgms)
01_prj_depends:=$(strip $(01_prj_depends))

# All the internal Projects that the test of the current Project depends on
01_test_depends:=$(project_test_int_static_libs) $(project_test_int_shared_libs) $(project_test_int_just_copy) $(project_test_int_pgms)
01_test_depends:=$(strip $(01_test_depends))

# -D options used to compile the Project
01_final_prj_minus_d:=-DPRJ=\"$(00_project)\" $(coc_prj_minus_d) $(coc_minus_d) $(project_prj_minus_d) $(01_directory_minus_d)
01_final_prj_minus_d:=$(strip $(01_final_prj_minus_d))

# -D options passed to compile the Test of the Project
01_final_test_minus_d:=-DPRJ=\"$(00_project)\" $(coc_test_minus_d) $(coc_minus_d) $(project_test_minus_d) $(01_directory_minus_d)
01_final_test_minus_d:=$(strip $(01_final_test_minus_d))

##### Paths

# all the include directories for the Project
01_final_prj_inc_dir:=$(coc_prj_ext_inc_dir) $(project_prj_ext_inc_dir) $(00_dir_dev_inc) $(00_dir_dev_src)
01_final_prj_inc_dir:=$(strip $(01_final_prj_inc_dir))
01_final_prj_inc_dir:=$(01_final_prj_inc_dir:%=$(coc_inc_dir_flag)%)

# all the include directories for the Test
01_final_test_inc_dir:=$(coc_test_ext_inc_dir) $(project_test_ext_inc_dir) $(00_dir_dev_inc) $(00_dir_dev_test)
01_final_test_inc_dir:=$(01_final_test_inc_dir:%=$(coc_inc_dir_flag)%)
01_final_test_inc_dir:=$(01_final_prj_inc_dir) $(01_final_test_inc_dir) 
01_final_test_inc_dir:=$(strip $(01_final_test_inc_dir))

# all the shared library directories for the Project
01_final_prj_lib_dir:=$(00_dir_product_lib) $(coc_prj_ext_lib_dir)
01_final_prj_lib_dir:=$(01_final_prj_lib_dir:%=$(coc_lib_dir_flag) %)
01_final_prj_lib_dir:=$(strip $(01_final_prj_lib_dir))

# all the shared library directories for the Test
01_final_test_lib_dir:=$(coc_test_ext_lib_dir) $(project_test_ext_lib_dir)
01_final_test_lib_dir:=$(01_final_prj_lib_dir) $(01_final_test_lib_dir:%=$(coc_lib_dir_flag) %)
01_final_test_lib_dir:=$(strip $(01_final_test_lib_dir))

##### Shared Libraries

# all the shared libraries referenced by the Project
01_final_prj_shared_libs:=$(coc_prj_ext_shared_libs) $(project_prj_ext_shared_libs) $(project_prj_int_shared_libs)
01_final_prj_shared_libs:=$(01_final_prj_shared_libs:%=$(coc_shared_lib_flag)%)
01_final_prj_shared_libs:=$(strip $(01_final_prj_shared_libs))

# all the shared libraries referenced by the Test
01_final_test_shared_libs:=$(coc_test_ext_shared_libs) $(project_test_ext_shared_libs) $(project_test_int_shared_libs)
01_final_test_shared_libs:=$(01_final_test_shared_libs:%=$(coc_shared_lib_flag)%)
01_final_test_shared_libs:=$(01_final_prj_shared_libs) $(01_final_test_shared_libs)
01_final_test_shared_libs:=$(strip $(01_final_test_shared_libs))

##### Static Libraries

# all the static libraries referenced by the Project
01_final_prj_static_libs:=$(coc_prj_ext_static_libs) $(project_prj_ext_static_libs) $(project_prj_int_static_libs:%=$(00_dir_product_lib)/$(coc_static_lib_prefix)%$(coc_static_lib_sufix))
01_final_prj_static_libs:=$(strip $(01_final_prj_static_libs))

# all the static libraries referenced by the Test
01_final_test_static_libs:=$(coc_test_ext_static_libs) $(project_test_ext_static_libs) $(project_test_int_static_libs:%=$(00_dir_product_lib)/$(coc_static_lib_prefix)%$(coc_static_lib_sufix))
01_final_test_static_libs:=$(01_final_prj_static_libs) $(01_final_test_static_libs)
01_final_test_static_libs:=$(strip $(01_final_test_static_libs))

##### Programs

# all the programs referenced by the Project
01_final_prj_pgms:=$(project_prj_int_pgms:%=$(coc_program_prefix)%$(coc_program_sufix)) $(coc_prj_ext_pgms) $(project_prj_ext_pgms)
01_final_prj_pgms:=$(strip $(01_final_prj_pgms))

# all the programs referenced by the Test
01_final_test_pgms:=$(project_test_int_pgms:%=$(coc_program_prefix)%$(coc_program_sufix)) $(coc_test_ext_pgms) $(project_test_ext_pgms)
01_final_test_pgms:=$(01_final_prj_pgms) $(01_final_test_pgms)
01_final_test_pgms:=$(strip $(01_final_test_pgms))

##### Just-copy

# all the just-copy referenced by the Project
01_final_prj_just_copy:=$(project_prj_int_just_copy)
01_final_prj_just_copy:=$(strip $(01_final_prj_just_copy))

##### Qt files to C++ files

# automatic list of qt user interface code files
#qt_code:=$(shell ls $(00_dir_dev_src)/*.$(qt_extension) 2> /dev/null)
#qt_code:=$(strip $(qt_code))

# automatic list of Project source files based on the Qt header files
#qt_code_cpps:=$(subst .$(qt_extension),.$(coc_source_extension),$(qt_code))
#qt_code_cpps:=$(strip $(qt_code_cpps))


# automatic list of Project header files based on the Qt header files
#qt_code_hs:=$(subst .$(qt_extension),.$(coc_header_extension),$(qt_code))
#qt_code_hs:=$(strip $(qt_code_hs))

# automatic list of qt user interface test files
#qt_test:=$(shell ls $(00_dir_dev_code)/test/*.$(qt_extension) 2> /dev/null)
#qt_test:=$(strip $(qt_test))

# automatic list of Test header files based on the Qt header files
#qt_test_hs:=$(subst .$(qt_extension),.$(coc_header_extension), $(qt_test))
#qt_test_hs:=$(strio $(qt_test_hs))

# automatic list of Test header source based on the Qt header files
#qt_test_cpps:=$(subst .$(qt_extension),.$(coc_source_extension), $(qt_test))
#qt_test_cpps:=$(strip $(qt_test_cpps))


##### Source and Object list

# automatic list of C++ source files for the Code
01_code_cpp_srcs:=$(wildcard $(00_dir_dev_src)/*.$(coc_source_extension)) 
#$(qt_code_cpps)
01_code_cpp_srcs:=$(strip $(01_code_cpp_srcs)) 

# automatic list of C++ object files for the Code
tmp_cpp_code_objs:=$(01_code_cpp_srcs:.$(coc_source_extension)=.o)
01_code_cpp_objs:=$(subst /src,/obj,$(tmp_cpp_code_objs))
01_code_cpp_objs:=$(strip $(01_code_cpp_objs))

# automatic list of C source files for the Code
01_code_c_srcs:=$(wildcard $(00_dir_dev_src)/*.c)
01_code_c_srcs:=$(strip $(01_code_c_srcs))

# automatic list of C object files for the Code
tmp_c_code_objs:=$(01_code_c_srcs:.c=.o)
01_code_c_objs:=$(subst /src,/obj,$(tmp_c_code_objs))
01_code_c_objs:=$(strip $(01_code_c_objs))

# automatic list of all the source files for the Code
01_code_srcs:=$(01_code_cpp_srcs)
01_code_srcs:=$(strip $(01_code_srcs))
#$(01_code_c_srcs)

# automatic list of all the object files for the Code
01_code_objs:=$(01_code_cpp_objs)
01_code_objs:=$(strip $(01_code_objs))
#$(01_code_c_objs)

01_code_depends=${01_code_objs:.o=.d}

# automatic list of source files for the Test
01_test_srcs:=$(wildcard $(00_dir_dev_test)/*.$(coc_source_extension)) $(qt_test_cpps)
01_test_srcs:=$(strip $(01_test_srcs)) 
#$(01_code_srcs)

# atomatic list of object files for the Test
t_objs_test:=$(01_test_srcs:.$(coc_source_extension)=.o)
#01_test_objs:=$(subst $(00_dir_dev_test)src/$(00_project)/test/,bin/$(00_project)/test/$(00_coc)/,$(t_objs_test)) $(01_code_objs)
#01_test_objs:=$(strip $(project_test_objs))
01_test_objs:=$(strip $(t_objs_test)) $(01_code_objs)


01_test_depends=${01_test_objs:.o=.d}


# **************  creates the necessary directories for a project
# this is not a 'public' command, therefore, it must not be used directly
01_setup : 01_setup_product 01_setup_dev

01_setup_product:
	$(00_show) ""                                                            ; \
	mkdir -p $(00_dir_product_bin)                              2> /dev/null ; \
	mkdir -p $(00_dir_product_lib)                              2> /dev/null ; \
	mkdir -p $(00_dir_product_cfg)                              2> /dev/null ; \
	mkdir -p $(00_dir_product_doc)                              2> /dev/null ; \
	mkdir -p $(00_dir_product_input)                            2> /dev/null ; \
	mkdir -p $(00_dir_product_output)                           2> /dev/null ; \
	mkdir -p $(00_dir_product_tmp)                              2> /dev/null ; \
	mkdir -p $(00_dir_product_log)                              2> /dev/null ; 

# 	mkdir -p $(dir_product)                                  2> /dev/null ; \


01_setup_dev:
	$(00_show) ""                                                            ; \
	mkdir -p $(00_dir_dev)                                      2> /dev/null ; \
	mkdir -p $(00_dir_dev_codes)                                2> /dev/null ; \
	mkdir -p $(00_dir_dev_prjs)                                 2> /dev/null ; \
	mkdir -p $(00_dir_dev_prj)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_code)                                 2> /dev/null ; \
	mkdir -p $(00_dir_dev_src)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_obj)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_inc)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_doc)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_input)                                2> /dev/null ; \
	mkdir -p $(00_dir_dev_cfg)                                  2> /dev/null ; \
	mkdir -p $(00_dir_dev_test)                                 2> /dev/null ; \
	touch $(00_dir_dev_input)/dummy                             2> /dev/null ; \
	touch $(00_dir_dev_cfg)/dummy                               2> /dev/null ; \
	touch $(00_dir_dev_doc)/dummy                               2> /dev/null ; \
	if [ ! -f "$(00_dir_dev_prj)/project.mk" ]; then                          \
		cp -f "$(00_dir_tool)/aux_files/project.mk"                       \
			"$(00_dir_dev_prj)/project.mk"              2> /dev/null ; \
	fi                                                                   


# **************  executes a target for all the references
01_prj_chain :
	$(00_show) ""                                                           ; \
	refs=`echo "$(01_prj_depends)" | tr -s " "`                             ; \
	refs=`echo "$$refs" | tr [:space:] "-"`                              ; \
	if [ ! -z $$refs ]; then                                               \
		cont_ref=1                                                   ; \
		ref=`echo $$refs | cut -d '-' -f$$cont_ref`                  ; \
		while [ ! -z "$$ref" ]; do                                     \
			make project="$$ref" coc=$(00_coc)        \
				verbose=$(00_verbose) target=$(00_target)            \
				chain_target=$(chain_target)                   \
				M_version=$(00_M_version) m_version=$(00_m_version)  \
				build_number=$(00_build_number)  01_prj_chain      ; \
			make project="$$ref" coc=$(00_coc)        \
				verbose=$(00_verbose) target=$(00_target)            \
				M_version=$(00_M_version) m_version=$(00_m_version)  \
				build_number=$(00_build_number) $(chain_target) ; \
			cont_ref=`echo $$cont_ref+1 | bc`                    ; \
			ref1=`echo $$refs | cut -d '-' -f$$cont_ref`         ; \
			if [ "$$ref" = "$$ref1" -a $$cont_ref -eq 2 ] ; then   \
				ref=""                                       ; \
			else                                                   \
				ref=$$ref1                                   ; \
			fi                                                   ; \
		done                                                         ; \
	fi                                                                   ;

# **************  executes a target for all the references
01_test_chain :
	$(00_show) ""                                                           ; \
	refs=`echo "$(01_test_depends)" | tr -s " "`                            ; \
	refs=`echo "$$refs" | tr [:space:] "-"`                              ; \
	if [ ! -z $$refs ]; then                                               \
		cont_ref=1                                                   ; \
		ref=`echo $$refs | cut -d '-' -f$$cont_ref`                  ; \
		while [ ! -z "$$ref" ]; do                                     \
			make project="$$ref" coc=$(00_coc)        \
				verbose=$(00_verbose) target=$(00_target)            \
				chain_target=$(chain_target)                   \
				M_version=$(00_M_version) m_version=$(00_m_version)  \
				build_number=$(00_build_number)  01_test_chain     ; \
			make project="$$ref" coc=$(00_coc)        \
				verbose=$(00_verbose) target=$(00_target)            \
				M_version=$(00_M_version) m_version=$(00_m_version)  \
				build_number=$(00_build_number) $(chain_target) ; \
			cont_ref=`echo $$cont_ref+1 | bc`                    ; \
			ref1=`echo $$refs | cut -d '-' -f$$cont_ref`         ; \
			if [ "$$ref" = "$$ref1" -a $$cont_ref -eq 2 ] ; then   \
				ref=""                                       ; \
			else                                                   \
				ref=$$ref1                                   ; \
			fi                                                   ; \
		done                                                         ; \
	fi                                                                   ;






-include $(01_code_depends)
-include $(01_test_depends)
